﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using App.Utilities.Web.Handlers;
using System.Data;
using CRMBiz;
using Newtonsoft.Json;
using CRMWeb.Utilities.Context;

namespace CRMWeb.WebServices
{
    /// <summary>
    /// Summary description for ApplicationContact
    /// </summary>
    public class ApplicationContact : BaseHandler
    {

        public override void OnMethodInvoke(OnMethodInvokeArgs e)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            context.Response.Cache.SetExpires(DateTime.MinValue);
            context.Response.Cache.SetNoStore();

            //e.Cancel = true;

            SetResponseContentType(ResponseContentTypes.HTML);
        }

        public override void AfterMethodInvoke(object result)
        {
            base.AfterMethodInvoke(result);
        }

        //user defined function
        #region private method

        public void ApplicationContactSelect(int ContactID)
        {
            try
            {
                string OperatorId = CMSContext.CurrentUser.NodeID.ToString();
                DataSet ds = ApplicationContactHandler.GetApplicationContact(ContactID,OperatorId);
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                context.Response.Write(strJson);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void SelectClientDetail(int ContactID)
        {
            string OperatorId = CMSContext.CurrentUser.NodeID.ToString();
            bool isDuplicate = false;
            try
            {
                if (ContactID > 0)
                {
                    DataSet ds = ApplicationContactHandler.GetApplicationContact(ContactID,OperatorId);
                    if ((ds.Tables[0].Rows[0]["deletedFlag"].ToString() == "False") && (Convert.ToInt32(ds.Tables[0].Rows[0]["dupwith"].ToString()) <= 0))
                    {
                        string bdate = ds.Tables[0].Rows[0]["DateBirth"].ToString();
                        string[] strdate = bdate.Split(' ');

                        isDuplicate = ApplicationContactHandler.CheckForDuplicate(ds.Tables[0].Rows[0]["FirstName"].ToString(),
                             ds.Tables[0].Rows[0]["SurName"].ToString(),
                             strdate[0],
                             ds.Tables[0].Rows[0]["Email"].ToString(),
                             ds.Tables[0].Rows[0]["secondEmailAddress"].ToString(),
                             ds.Tables[0].Rows[0]["Mobile"].ToString(),
                             ds.Tables[0].Rows[0]["Street"].ToString(),
                             ds.Tables[0].Rows[0]["State"].ToString(),
                             ds.Tables[0].Rows[0]["Suburb"].ToString());

                    }
                    ds.Tables[0].Columns.Add("isDuplicate");
                    if (isDuplicate == true)
                        ds.Tables[0].Rows[0]["isDuplicate"] = "1";
                    else
                        ds.Tables[0].Rows[0]["isDuplicate"] = "0";

                    string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                    context.Response.Write(strJson);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void GetApplicationForClient(int ContactID)
        {
            try
            {
                DataSet ds = ApplicationContactHandler.GetApplicationForClient(ContactID);
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                context.Response.Write(strJson);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void ApplicationContactSelectAll()
        {
            try
            {
                var pageIndex = string.IsNullOrEmpty(context.Request["page"]) ? 1 : int.Parse(context.Request["page"]);
                var pageSize = string.IsNullOrEmpty(context.Request["rows"]) ? 10 : int.Parse(context.Request["rows"]);
                var searchtext = string.IsNullOrEmpty(context.Request.QueryString["searchtext"]) ? "" : context.Request.QueryString["searchtext"];
                var conditiontext = string.IsNullOrEmpty(context.Request.QueryString["conditiontext"]) ? "" : context.Request.QueryString["conditiontext"];
                var sortText = string.IsNullOrEmpty(context.Request.QueryString["sortCondition"]) ? "" : context.Request.QueryString["sortCondition"];
                var days = string.IsNullOrEmpty(context.Request.QueryString["days"]) ? "All" : context.Request.QueryString["days"];
                DataSet ds = ApplicationContactHandler.GetApplicationContactList(pageIndex, pageSize, searchtext, conditiontext, sortText, days);

                var total = (ds.Tables[0].Rows.Count > 0) ? Convert.ToInt32(ds.Tables[0].Rows[0]["Total"]) : 0;
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                strJson = "{\"total\":\"" + total.ToString() + "\",\"rows\":" + strJson + "}";
                context.Response.Write(strJson);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void GetAppLoanRecord(int ContactID, string sortText)
        {
            try
            {
                if (ContactID > 0)
                {
                    DataSet ds = ApplicationContactHandler.GetAppLoanRecord(ContactID, sortText);
                    string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                    context.Response.Write(strJson);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void CheckContactID(int ContactID)
        {
            try
            {
                string strJson = string.Empty;
                bool flag = ApplicationContactHandler.CheckContactID(ContactID);
                if (flag == true)
                    strJson = "true";
                else
                    strJson = " false";
                context.Response.Write(strJson);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void SelectApplicationByAppID(int app_id)
        {
            try
            {
                DataSet ds = ApplicationDetailHandler.SelectApplicationDetail(app_id);
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                context.Response.Write(strJson);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void BindApplicationContactForm(int ContactID)
        {
            try
            {
                DataSet ds = ApplicationContactHandler.GetApplicationContactForm(ContactID);
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                context.Response.Write(strJson);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void ApplicationContactAdd(string Title, string FirstName, string MiddleName, string SurName, string Citizenship, string DateBirth, string DriverLicense, string Mobile, string HomePhone,
            string Email, string secondEmailAddress, string StreetNo, string Street, string Suburb, string State, string PostCode,
            string app_ResidentialStatus, string ResidencePeriod, string ResidenceLength, string Unsubscribe_Ad, string Dependant, string Memo, string Operator)
        {
            try
            {
                DataSet ds = ApplicationContactHandler.AddApplicationContact(Title, FirstName, MiddleName, SurName, Citizenship, DateBirth, DriverLicense, Mobile, HomePhone, Email, secondEmailAddress, StreetNo, Street, Suburb, State, PostCode, app_ResidentialStatus, ResidencePeriod, ResidenceLength, Unsubscribe_Ad, Dependant, Memo, Operator);
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                context.Response.Write(strJson);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public int PartiesContactAdd(int AppID, string Title, string FirstName, string MiddleName, string SurName, string Role, string Relationship, string DateBirth, string MobilePhone, string HomePhone, string WorkPhone, string FaxNumber, string Email1, string Email2, string Street, string Suburb, string State, string PostCode, string PostalAddress, string IncomeType, string TypeofEntity, string BusinessName, string ABN, string NatureofBusiness, string Phone, string Address, string Employer, string Occupation, string JobPosition, string EmployerPhone, string ContactPerson, string WorkAddress, string KinsName, string KinsRelationship, string ContactNumber, string Notes)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = ApplicationContactHandler.PartiesContactAdd(AppID, Title, FirstName, MiddleName, SurName, Role, Relationship, DateBirth, MobilePhone, HomePhone, WorkPhone, FaxNumber, Email1, Email2, Street, Suburb, State, PostCode, PostalAddress, IncomeType, TypeofEntity, BusinessName, ABN, NatureofBusiness, Phone, Address, Employer, Occupation, JobPosition, EmployerPhone, ContactPerson, WorkAddress, KinsName, KinsRelationship, ContactNumber, Notes);
            }
            catch (Exception)
            {
                throw;
            }
            return intReturnValue;
        }
        public int ApplicationContactEdit(int ContactID, string Title, string FirstName, string MiddleName, string SurName, string Citizenship, string DateBirth,
            string DriverLicense, string Mobile, string HomePhone, string Email, string secondEmailAddress, string StreetNo, string Street, string Suburb,
            string State, string PostCode, string app_ResidentialStatus, string ResidencePeriod, string ResidenceLength, string Unsubscribe_Ad,
            string Dependant, string Memo, string Operator)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = ApplicationContactHandler.EditApplicationContact(ContactID, Title, FirstName, MiddleName, SurName, Citizenship, DateBirth, DriverLicense, Mobile, HomePhone, Email, secondEmailAddress, StreetNo, Street, Suburb, State, PostCode, app_ResidentialStatus, ResidencePeriod, ResidenceLength, Unsubscribe_Ad, Dependant, Memo, Operator);
            }
            catch (Exception)
            {
                throw;
            }
            return intReturnValue;
        }
        public int PartiesContactEdit(int ContactID, string Title, string FirstName, string MiddleName, string SurName, string Role, string Relationship, string DateBirth, string MobilePhone, string HomePhone, string WorkPhone, string FaxNumber, string Email1, string Email2, string Street, string Suburb, string State, string PostCode, string PostalAddress, string IncomeType, string TypeofEntity, string BusinessName, string ABN, string NatureofBusiness, string Phone, string Address, string Employer, string Occupation, string JobPosition, string EmployerPhone, string ContactPerson, string WorkAddress, string KinsName, string KinsRelationship, string ContactNumber, string Notes)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = ApplicationContactHandler.PartiesContactEdit(ContactID, Title, FirstName, MiddleName, SurName, Role, Relationship, DateBirth, MobilePhone, HomePhone, WorkPhone, FaxNumber, Email1, Email2, Street, Suburb, State, PostCode, PostalAddress, IncomeType, TypeofEntity, BusinessName, ABN, NatureofBusiness, Phone, Address, Employer, Occupation, JobPosition, EmployerPhone, ContactPerson, WorkAddress, KinsName, KinsRelationship, ContactNumber, Notes);
            }
            catch (Exception)
            {
                throw;
            }
            return intReturnValue;
        }
        public bool RemoveDuplicateClient(int CurrentContactID, int DupWithContactID)
        {
            bool flag = false;
            try
            {
                flag = ApplicationContactHandler.RemoveDuplicateContact(CurrentContactID, DupWithContactID);
            }
            catch (Exception)
            {
                throw;
            }
            return flag;
        }
        public bool DeleteClient(int ContactID)
        {
            bool flag = false;
            try
            {
                flag = ApplicationContactHandler.deleteClient(ContactID);
            }
            catch (Exception)
            {
                throw;
            }
            return flag;
        }
        public int DeleteParties(int ContactID)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = ApplicationContactHandler.DeleteParties(ContactID);
            }
            catch (Exception)
            {
                throw;
            }
            return intReturnValue;
        }

        #endregion
    }
}